package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.tmall.dal.entity.TmallOrd;
import com.cts.tmall.dal.mapper.TmallOrdMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Map;

@Service
public class OrdService extends BaseService<TmallOrd>{

    @Autowired
    private TmallOrdMapper ordMapper;

    public IPage<TmallOrd> queryOrd(Map<String, String> params) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Page<TmallOrd> page = new Page<>(
                StringUtils.isBlank(params.get("pageNumb")) ? 1L : Long.parseLong(params.get("pageNumb")),
                StringUtils.isBlank(params.get("pageSize")) ? 10L : Long.parseLong(params.get("pageSize"))
        );
        LambdaQueryWrapper<TmallOrd> wrapper = Wrappers.<TmallOrd>lambdaQuery()
                .eq(StringUtils.isNotBlank(params.get("ordNo")), TmallOrd::getOrdNo, params.get("ordNo"))
                .eq(StringUtils.isNotBlank(params.get("ordStatus")), TmallOrd::getOrdStatus, params.get("ordStatus"))
                .eq(StringUtils.isNotBlank(params.get("afterSaleStatus")), TmallOrd::getAfterSaleStatus, params.get("afterSaleStatus"))
                .eq(StringUtils.isNotBlank(params.get("bizType")), TmallOrd::getBizType, params.get("bizType"))
                .eq(StringUtils.isNotBlank(params.get("payType")), TmallOrd::getPayType, params.get("payType"))
                .eq(StringUtils.isNotBlank(params.get("prdNo")), TmallOrd::getPrdNo, params.get("prdNo"))
                .eq(StringUtils.isNotBlank(params.get("expressOrg")), TmallOrd::getExpressOrg, params.get("expressOrg"))
                .eq(StringUtils.isNotBlank(params.get("expressOrderNo")), TmallOrd::getExpressOrderNo, params.get("expressOrderNo"))
                .eq(StringUtils.isNotBlank(params.get("customName")), TmallOrd::getCustomName, params.get("customName"))
                .eq(StringUtils.isNotBlank(params.get("customTel")), TmallOrd::getCustomTel, params.get("customTel"))
                .eq(StringUtils.isNotBlank(params.get("isPresell")), TmallOrd::getIsPresell, params.get("isPresell"))
                .eq(StringUtils.isNotBlank(params.get("prdType")), TmallOrd::getPrdType, params.get("prdType"))
                .eq(StringUtils.isNotBlank(params.get("appChanne")), TmallOrd::getAppChannel, params.get("appChanne"))
                .eq(StringUtils.isNotBlank(params.get("sendStatus")), TmallOrd::getSendStatus, params.get("sendStatus"))
                .eq(StringUtils.isNotBlank(params.get("refundStatus")), TmallOrd::getRefundStatus, params.get("refundStatus"))
                .eq(StringUtils.isNotBlank(params.get("customWish")), TmallOrd::getCustomWish, params.get("customWish"));
        if (StringUtils.isNotBlank(params.get("createTime1"))) {
            wrapper.ge(TmallOrd::getCreateTime, sdf.parse(params.get("createTime1")));
        }
        if (StringUtils.isNotBlank(params.get("createTime2"))) {
            wrapper.le(TmallOrd::getCreateTime, sdf.parse(params.get("createTime2")));
        }
        if (StringUtils.isNotBlank(params.get("finishTime1"))) {
            wrapper.ge(TmallOrd::getCreateTime, sdf.parse(params.get("finishTime1")));
        }
        if (StringUtils.isNotBlank(params.get("finishTime2"))) {
            wrapper.le(TmallOrd::getCreateTime, sdf.parse(params.get("finishTime2")));

        }
        return ordMapper.selectPage(page, wrapper);
    }
}
